package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author kabuda
 * @create 2022-08-24 10:43
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        //如果是提交有密码的话就加密保存到数据库
        String password = member.getPassword();
        if (password != null) {
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public List findByRegTime(List<String> months) {
        List count = new ArrayList();
        for (String month : months) {
            String isMon = month.substring(month.lastIndexOf("-")+1);
            //31天
            if ("01".equals(isMon) || "03".equals(isMon) || "05".equals(isMon) || "07".equals(isMon) || "08".equals(isMon) || "10".equals(isMon) || "12".equals(isMon)) {
                count.add(memberDao.findMemberCountBeforeDate(month + "-31"));
            } else if ("04".equals(isMon) || "06".equals(isMon) || "09".equals(isMon) || "11".equals(isMon)) {
                count.add(memberDao.findMemberCountBeforeDate(month + "-30"));
            }

            int num = Integer.parseInt(month.substring(0,month.lastIndexOf("-")));

            if ("02".equals(isMon)) {
                if (num % 4 == 0 && num % 100 != 0 || num % 400 == 0) {
                    count.add(memberDao.findMemberCountBeforeDate(month + "-29"));
                } else {
                    count.add(memberDao.findMemberCountBeforeDate(month + "-28"));
                }
            }
        }
        return count;
    }

    @Override
    public List<Map<String, Object>> findStmealCount() {
        return memberDao.findStmealCount();
    }


}
